package com.bidevalution.business.impl;

import com.bidevalution.business.service.BeEvalutionExpertService;
import com.bidevalution.business.service.BeSectionExpertInfoService;
import com.bidevalution.business.service.BeSectionExpertProgressService;
import com.bidevalution.business.service.BeSectionStepJobService;
import com.bidevalution.business.service.BeSectionStepService;
import com.bidevalution.business.service.JdbcTemplateService;
import com.bidevalution.dao.entity.BeEvalutionExpert;
import com.bidevalution.dao.entity.BeSectionExpertInfo;
import com.bidevalution.dao.entity.BeSectionExpertProgress;
import com.bidevalution.dao.entity.BeSectionStep;
import com.bidevalution.dao.extend.BeEvalutionExpertExt;
import com.bidevalution.dao.mapper.BeEvalutionExpertMapper;
import com.bidevalution.service.layout.ColumnHeader;
import com.bidevalution.support.context.Constant;
import com.bidevalution.support.context.ResponseObject;
import com.bidevalution.support.context.WebSocketOpTypeEnum;
import com.bidevalution.support.context.Constant.EnterpriseEnum;
import com.bidevalution.support.factory.WebSocketService;
import com.bidevalution.support.util.MapUtil;
import com.bidevalution.support.util.StringUtil;
import com.bidevalution.support.websocket.WebSocketCached;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.qtp.core.model.Response;
import com.qtp.core.util.IdsUtil;
import com.qtp.core.util.OConvertUtils;import javassist.expr.NewArray;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * Created by glodon on 2019-08-22.
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BeEvalutionExpertServiceImpl implements BeEvalutionExpertService {
	@Autowired
	private BeEvalutionExpertMapper beEvalutionExpertMapper;
	@Autowired
	private BeSectionExpertInfoService beSectionExpertInfoService;
	@Autowired
	private WebSocketService webSocketService;
	@Autowired
	private BeSectionStepService beSectionStepService;
	@Autowired
	private JdbcTemplateService jdbcTemplateService;

	/**
	 * 保存专家评审结果
	 * 
	 * @param beEvalutionExpertList
	 * @param sectionStepId
	 * @param userGuid
	 * @param isFinish
	 * @return
	 * @throws Exception
	 */
	@Override
	@Transactional
	public void saveEvalutionExpert(List<BeEvalutionExpert> beEvalutionExpertList, BeSectionStep beSectionStep,
			String expertId, String submitCode, String isFinish, List<String> rowKeyList) throws Exception {
		if (beEvalutionExpertList.size() == 0) {
			return;
		}
		if (rowKeyList.size() == 0) {
			beEvalutionExpertMapper.deleteExpertSubmitList(beSectionStep.getSeqId(), expertId, submitCode, null);
		} else {
			for (String rowKey : rowKeyList) {
				// 先删除后新增
				beEvalutionExpertMapper.deleteExpertSubmitList(beSectionStep.getSeqId(), expertId, submitCode, rowKey);
			}
		}
		
		beEvalutionExpertMapper.bathInsert(beEvalutionExpertList);
		// 通知其他人员
		if ("1".equals(isFinish)) {
			// 一个专家完成提交，则通知其他专家
			for (BeSectionExpertInfo sectionExpertInfo : beSectionExpertInfoService
					.getAllListBySectionId(beSectionStep.getSectionId())) {
				System.out.println(beSectionStep.getSeqId() + "发送websocket:"
						+ WebSocketCached.getExpertChannelContextMap().get(sectionExpertInfo.getSeqId())
						+ sectionExpertInfo.getSeqId());
				// 发送websocket消息
				if (WebSocketCached.getExpertChannelContextMap().get(sectionExpertInfo.getSeqId()) != null) {
					List<String[]> md5ByIdCards = beSectionExpertInfoService.findMd5ByLoginNames(beSectionStep.getSectionId());
					webSocketService.send(
							WebSocketCached.getExpertChannelContextMap().get(sectionExpertInfo.getSeqId()),
							new ResponseObject(WebSocketOpTypeEnum.EXPERT_FINISH).setTodoList(md5ByIdCards));
				}
			}
		}
	}

	@Override
	public void deleteBySectionId(String sectionId) {
		beEvalutionExpertMapper.deleteBySectionId(sectionId);
	}

	@Override
	public void save(List<Map<String, Object>> dataList, List<ColumnHeader> columnList, String sectionStepId,
			List<BeSectionExpertInfo> expertInfoList, String layoutCode, String submitCode, String isFinish)
			throws Exception {
		String rowKey = "";// 每行数据的唯一值
		BeEvalutionExpert beEvalutionExpert;
		BeSectionStep beSectionStep = beSectionStepService.getSectionStepBySeqId(sectionStepId);
		List<String> rowKeyList = new ArrayList<>();
		List<BeEvalutionExpert> beEvalutionExperts;
		for (BeSectionExpertInfo beSectionExpertInfo : expertInfoList) {
			beEvalutionExperts = Lists.newArrayList();
			for (Map<String, Object> dataMap : dataList) {
				if (!StringUtils.isEmpty(dataMap.get(Constant.EnterpriseEnum.EnterpriseId.getCode()))) {
					if (StringUtils.isEmpty(dataMap.get("rowKey"))) {
						rowKey = IdsUtil.uuid().replaceAll("-", "");
					} else {
						rowKey = OConvertUtils.getString(dataMap.get("rowKey"));
						rowKeyList.add(rowKey);
					}
					for (ColumnHeader columnHeader : columnList) {
						if (!("rowKey").equals(columnHeader.getName())) {
							beEvalutionExpert = new BeEvalutionExpert();
							beEvalutionExpert.setSeqId(IdsUtil.uuid());
							beEvalutionExpert.setEnterpriseId(
									StringUtil.toString(dataMap.get(EnterpriseEnum.EnterpriseId.getCode())));
							beEvalutionExpert.setSectionStepId(sectionStepId);
							beEvalutionExpert.setSectionStepCode(beSectionStep.getStepCode());
							beEvalutionExpert.setSectionId(beSectionStep.getSectionId());
							beEvalutionExpert.setExpertId(beSectionExpertInfo.getSeqId());
							beEvalutionExpert.setLayoutCode(layoutCode);
							beEvalutionExpert.setRowKey(rowKey);
							beEvalutionExpert.setSubmitCode(submitCode);
							beEvalutionExpert.setColumnCode(columnHeader.getCode());
							beEvalutionExpert
									.setColumnValue(OConvertUtils.getString(dataMap.get(columnHeader.getCode())));
							beEvalutionExperts.add(beEvalutionExpert);
						}
					}
				}

			}
			saveEvalutionExpert(beEvalutionExperts, beSectionStep, beSectionExpertInfo.getSeqId(), submitCode, isFinish,
					rowKeyList);
			
		}
		
	}

	@Override
	public int selectCount(String sectionStepId, String layoutCode) {

		return beEvalutionExpertMapper.seclectCount(sectionStepId, layoutCode);
	}

}